﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HousingInfo.Library;

namespace NEWS.Admin
{
    public partial class PriceAdvertisement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtpriceid.Text = GridView1.SelectedRow.Cells[2].Text;
            txtpricename.Text = GridView1.SelectedRow.Cells[3].Text;
            lblpriceID.Visible = true;
            txtpriceid.Visible = true;
            txtgia.Text = GridView1.SelectedRow.Cells[4].Text;
            PanelActions.Visible = true;
            message.Text = "";
            btnAdd.Visible = false;
            btnUpdate.Visible = true;
            btnDelete.Visible = true;
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int priceid = Convert.ToInt32(txtpriceid.Text);
            PriceAdvertisementBO pricebo = PriceAdvertisementBO.GetPriceAdvertisementBO(priceid);
            pricebo.PriceAdvertisementName = txtpricename.Text;
            pricebo.Price = Convert.ToInt32(txtgia.Text);
            try
            {
                pricebo.Save();
                message.Text = "Cập nhật thành công !";
                GridView1.DataBind();
            }
            catch
            {
                message.Text = "Cập nhật thất bại!";
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int priceid = Convert.ToInt32(txtpriceid.Text);
            try
            {
                PriceAdvertisementBO.DeletePriceAdvertisementBO(priceid);
                messagedelete.Text = "Đã xoá";
                GridView1.DataBind();
                PanelActions.Visible = false;
            }
            catch
            {
                messagedelete.Text = "Xoá thất bại";
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            PriceAdvertisementBO priceboadd = PriceAdvertisementBO.NewPriceAdvertisementBO();
            priceboadd.PriceAdvertisementName = txtpricename.Text;
            priceboadd.Price = Convert.ToInt32(txtgia.Text);
            try
            {
                priceboadd.Save();
                message.Text = "Thêm thành công";
                GridView1.DataBind();
            }
            catch
            {
                message.Text = "Thêm thất bại";
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string strScript = "SelectDeSelectHeader(" + ((CheckBox)e.Row.Cells[0].FindControl("chkSelect")).ClientID + ");";
                    ((CheckBox)e.Row.Cells[0].FindControl("chkSelect")).Attributes.Add("onclick", strScript);
                }
            }
            catch
            {
                //report error
            }  
        }

        protected void btnthemmoi_Click(object sender, EventArgs e)
        {
            lblpriceID.Visible = false;
            txtpriceid.Visible = false;
            messagedelete.Text = "";
            txtpricename.Text = "";
            txtgia.Text = "";
            btnUpdate.Visible = false;
            btnDelete.Visible = false;
            btnAdd.Visible = true;
            PanelActions.Visible = true;
            messagedelete.Text = "";
        }

        protected void btnxoa_Click(object sender, EventArgs e)
        {
            bool test = false;
            foreach (GridViewRow gvRow in GridView1.Rows)
            {
                CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
                if (chkSel.Checked == true)
                {
                    try
                    {
                        test = true;
                        int priceiddelete = Convert.ToInt32(gvRow.Cells[2].Text);                        
                        PriceAdvertisementBO.DeletePriceAdvertisementBO(priceiddelete);
                    }
                    catch
                    {
                        messagedelete.Text = "Xóa thất bại";
                        return;
                    }
                }
            }
            if (test == false)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Chưa chọn thông tin cần xóa !!!')", true);
                return;
            }
            else
            {
                messagedelete.Text = "Xóa thành công !";
                GridView1.DataBind();
                PanelActions.Visible = false;
            }
        }

        protected void btndong_Click(object sender, EventArgs e)
        {
            PanelActions.Visible = false;
        }
    }
}
